package apis

import (
	"errors"
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
)

type SysDictData struct {
	api.Api
}

func (e SysDictData) GetPage(c *gin.Context) {
	s := service.SysDictData{}
	req := query.SysDictDataGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	list := make([]models.SysDictData, 0)
	var count int64
	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Page(count, req.PageSize, req.PageIndex, list)
}

func (e SysDictData) Get(c *gin.Context) {
	s := service.SysDictData{}
	req := query.SysDictDataGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	var object models.SysDictData

	err = s.Get(&req, &object)
	if err != nil {
		e.Logger.Warnf("Get error: %s", err.Error())
		e.Fail(serrors.Fail)
		return
	}

	e.Data(object)
}

func (e SysDictData) Insert(c *gin.Context) {
	s := service.SysDictData{}
	req := command.SysDictDataInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetCreateBy(userId)
	err = s.Insert(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Data(req.GetId())
}

func (e SysDictData) Update(c *gin.Context) {
	s := service.SysDictData{}
	req := command.SysDictDataUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetUpdateBy(userId)
	err = s.Update(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}

func (e SysDictData) Delete(c *gin.Context) {
	s := service.SysDictData{}
	req := command.SysDictDataDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetUpdateBy(userId)
	err = s.Remove(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}

func (e SysDictData) GetAll(c *gin.Context) {
	s := service.SysDictData{}
	req := query.SysDictDataGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	list := make([]models.SysDictData, 0)
	err = s.GetAll(&req, &list)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	l := make([]query.SysDictDataGetAllResp, 0)
	for _, i := range list {
		d := query.SysDictDataGetAllResp{}
		e.Translate(i, &d)
		l = append(l, d)
	}

	e.Data(l)
}

// 变量导出
func (e SysDictData) ExportDictData(c *gin.Context) {
	req := query.SysDictDataGetPageReq{}
	// 业务service
	s := &service.SysDictData{}
	// 模型
	m := models.SysDictData{}
	// 上下文
	err := e.MakeContext(c).Bind(&req).MakeService(&s.Service).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	if len(req.SystemId) == 0 {
		err = errors.New("systemId 不能为空")
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 注入模型
	// 执行
	//req.LoadUser(c)
	err = s.ExportDictData(c, &m, &req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	c.Abort()
}

// 变量导入
func (e SysDictData) ImportDictData(c *gin.Context) {
	req := query.SysDictDataGetPageReq{}
	// 业务service
	s := &service.SysDictData{}
	// 模型
	m := models.SysDictData{}
	// 上下文
	err := e.MakeContext(c).Bind(&req, binding.FormMultipart).MakeService(&s.Service).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	if len(req.SystemId) == 0 {
		err = errors.New("systemId 不能为空")
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 注入模型
	//s.SetVariableModel(&m)
	// 执行
	//req.LoadUser(c)
	err = s.ImportDictData(c, &m, &req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 返回
	e.Ok(nil)
}
